using UnityEngine;
using System.Collections;

public class Weapon : MonoBehaviour
{
    public float damage = 10;
    public float radius = 10;
    public float attackSpeed = 10;
    public float minAttackInterval = 100;
    public float maxAttackInterval = 1000;
    public Transform explosionPrefab;
    public bool autoaim = false;
    public bool aimable = false;
    public ShieldClass disableShieldClass = ShieldClass.none;
    public float disableShieldClassTimespan = 0;
    public ShieldClass blockedByShieldClass = ShieldClass.none;
    public float liveStealPercentage;
    public float slowPercentage;
    public float slowTimespan;
    public int slowChance;
    public float stunTimespan;
    public int stunChance;
    public float poisonAmount;
    public float poisonTimespan;
    public float poisonDelay;
    public int poisonChance;
    public float decreaseAttackSpeedPercentage;
    public float decreaseAttackSpeedTimespan;
    public int decreaseAttackSpeedChance;
    public MovieTexture image;
    public int ammo = 100;
    [HideInInspector]
    public int count;

    public float force = 100;
    public GameObject sender;
    // Use this for initialization
   public void Init()
    {
        print("ammo"+ammo);
        count = ammo;
        print("count" + count);
    }
    public float getCurrentAttackInterval()
    {
        return (minAttackInterval + maxAttackInterval / attackSpeed) / 100;
    }
    void OnCollisionEnter(Collision collision)
    {
        var contact = collision.contacts[0];

        var rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
        var pos = contact.point;
        Network.Instantiate(explosionPrefab, pos, rot, 0);
        networkView.RPC("AreaDamageEnemies", RPCMode.Others, pos, sender.networkView.viewID);

        localAreaDamageEnemies(pos);
        // Destroy the projectile
        Network.Destroy(gameObject);

    }
    public float localAreaDamageEnemies(Vector3 location)
    {
        float dmg = 0;
        Collider[] objectsInRange = Physics.OverlapSphere(location, radius);
        foreach (Collider col in objectsInRange)
        {

            Controller enemy = (Controller)col.GetComponent("Controller");
            if (enemy != null)
            {
                dmg += enemy.ApplyDamage(Mathf.Abs(damage), this);
            }
        }
        Controller c = (Controller)sender.GetComponent("Controller");
        c.ApplyHealth(dmg);
        return dmg;
    }
    [RPC]
    public void AreaDamageEnemies(Vector3 location, NetworkViewID id)
    {
        float dmg = localAreaDamageEnemies(location);
        Controller c = (Controller)NetworkView.Find(id).gameObject.GetComponent("Controller");
        c.ApplyHealth(dmg);
    }

}
